﻿using System.Collections.Generic;

namespace UI
{
    using static Utilitys;

    public static class UIFormInfos
    {
        private static List<IUIFormInfoLoader> _loaders = new List<IUIFormInfoLoader>();
        internal static void AddLoader(IUIFormInfoLoader loader)
        {
            _loaders.Insert(0, loader);
        }

        public static UIFormInfo GetInfo(string formName)
        {
            UIFormInfo info = Default;
            for(int i = 0;i< _loaders.Count; ++i)
            {
                if (_loaders[i].LoadInfo(formName, ref info))
                {
                    return info;
                }
            }
            LoadInfo(formName, ref info);
            return info;
        }

        private static UIFormInfo Default
        {
            get { return UISystem.Settings.DefaultFormInfo; }
        }

        private static bool LoadInfo(string formName, ref UIFormInfo info)
        {
            var root = UISystem.Settings.UIFormRootPath;
            var path = formName;
            if (!string.IsNullOrEmpty(root))
            {
                path = $"{root}/{path}Info";
            }
            var asset = UISystem.LoadAsset<UIFormInfoAsset>(path);
            if (asset != null)
            {
                info = asset.Info;
                return true;
            }
            return false;
        }

    }
}